package com.lingx.web.core;

import java.io.File;
import java.io.FileOutputStream;
import java.text.MessageFormat;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.lingx.service.LingxService;
import com.lingx.utils.TokenUtils;
import com.lingx.utils.Utils;
import com.lingx.web.LingxApp;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
import io.netty.handler.codec.http.multipart.FileUpload;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
import io.netty.util.CharsetUtil;

public class FileUploadRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

	@Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
		LingxService lingx=LingxApp.SPRINGCONTEXT.getBean(LingxService.class);
		String pathRoot=lingx.getConfigValue("lingx.file.save.path", "");
		if(Utils.isNull(pathRoot)) {
			pathRoot=System.getProperty("user.dir")+"/files/";
		}
		System.out.println("Upload:"+pathRoot);
		DefaultHttpDataFactory factory = new DefaultHttpDataFactory(true);
        HttpPostRequestDecoder httpDecoder = new HttpPostRequestDecoder(factory, fullHttpRequest);
        httpDecoder.setDiscardThreshold(0);
        String token=null;
        final HttpContent chunk = fullHttpRequest;
        httpDecoder.offer(chunk);
        if (chunk instanceof LastHttpContent) {
            List<InterfaceHttpData> interfaceHttpDataList = httpDecoder.getBodyHttpDatas();
            for (InterfaceHttpData data : interfaceHttpDataList) {
            	System.out.println(data.getHttpDataType());
                if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload) {
                     if(Utils.isNull(token)) {
                     	System.out.println("token为空,上传失败");
                     	return;
                     }
                    FileUpload fileUpload = (FileUpload) data;
                    String path=saveFile(fileUpload.getFilename(),fileUpload.get().length,token,pathRoot);
                    try( FileOutputStream fileOutputStream = new FileOutputStream(pathRoot+path) ) {
                        fileOutputStream.write(fileUpload.get());
                        fileOutputStream.flush();
                    }
                }
                //如果数据类型为参数类型，则保存到body对象中
                if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute){
                    Attribute attribute = (Attribute) data;
                    token=attribute.getValue();
                    System.out.println(attribute.getName() + ":" + attribute.getValue());
                }
            }
        }

        FullHttpResponse response = new DefaultFullHttpResponse(
                io.netty.handler.codec.http.HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8));
        response.headers().set("Access-Control-Allow-Origin", "*");
		response.headers().add("Access-Control-Allow-Methods", "GET,POST");
		response.headers().set("Content-Type", "application/json; charset=UTF-8");
        response.headers().set("Content-Length", response.content().readableBytes());

        channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
	
	public String saveFile(String filename,long length,String token,String pathRoot) {
		String t_ext=this.getSuffix(filename);
		
		String path="upload/"+getDate() + System.currentTimeMillis() + "." + t_ext;
		try {
			File dir=new File(pathRoot+"upload/"+getDate());
			if(!dir.exists())dir.mkdirs();
			String userId=TokenUtils.getTokenDataUserId(token);
			//System.out.println(path);
			//System.out.println(length);
			//System.out.println(userId);
			//System.out.println(filename);
			JdbcTemplate jdbc=LingxApp.SPRINGCONTEXT.getBean(JdbcTemplate.class);
			jdbc.update("insert into tlingx_file(id,name,length,user_id,ts) values(?,?,?,?,?)",path,filename,length/1024,userId,Utils.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}

	public String getSuffix(String name){
		String suffix="";
		if(name.indexOf(".")>0){
			suffix=name.substring(name.lastIndexOf(".")+1);
		}
		return suffix;
	}
	public static String getDate() {
		String dateTime = MessageFormat.format("{0,date,yyyy/MM/dd/}",
				new Object[] { new java.sql.Date(System.currentTimeMillis()) });
		return dateTime;

	}
}

